<?php

/**
 * User: zhangligang
 * Email: tzhang2450@163.com
 * Date: 2018/7/27
 * Time: 上午6:43
 */

namespace App\Services;

use App\Models\Apart;
use App\Models\Program;
use App\Models\ProgramApart;
use App\Models\ProgramUser;
use App\Models\Region;
use App\Models\Standard;
use App\Models\User;
use App\Providers\SendSms;
use Illuminate\Support\Facades\Redis;

class Service
{

    public static function getRegions()
    {
        $regions = Region::where('status', 1)->get();
        $data = [];
        foreach ($regions as $region) {
            $data[] = [
                'id' => $region->id,
                'name' => $region->name
            ];
        }

        return ['rows' => $data];
    }

    public static function getStandards()
    {
        $standards = Standard::where('status', 1)->get();
        $data = [];
        foreach ($standards as $standard) {
            $data[] = [
                'id' => $standard->id,
                'name' => $standard->name
            ];
        }

        return ['rows' => $data];
    }

    public static function getRegionList($limit, $offset, $order = 'desc')
    {
        $query = Region::where('status', 1);
        $total = $query->count();
        $standards = $query->orderBy('id', $order)->limit($limit)->offset($offset)->get();
        $data = [];
        foreach ($standards as $standard) {
            $data[] = [
                'id'            => $standard->id,
                "name"          => $standard->name
            ];
        }

        return [
            'total'      => $total,
            'rows'       => $data,
            'limit'      => $limit,
            'offset'     => $offset,
            'page'       => ceil($total / $limit),
            'pageNumber' => $offset / $limit + 1
        ];
    }

    public static function getRegionDetail($id)
    {
        $region = Region::where('id', $id)->first();

        if (!$region) {
            return [];
        }

        return [
            'data' => [
                'id'            => $region->id,
                "name"          => $region->name
            ]
        ];
    }

    public static function saveRegion($data)
    {
        $region = new Region();
        if (array_key_exists('id', $data) && $data['id']) {
            $region = Region::where('id', $data['id'])->first();
        } else {
            $region->created_at = date('Y-m-d H:i:s', time());
        }
        $region->name = $data['name'];
        $region->updated_at = date('Y-m-d H:i:s', time());
        $region->save();
    }

    public static function deleteRegion($id)
    {
        $region = Region::where('id', $id)->first();
        $region->status = 0;
        $region->updated_at = date('Y-m-d H:i:s', time());
        $region->save();
    }

    public static function getStandardList($limit, $offset, $order = 'desc')
    {
        $query = Standard::where('status', 1);
        $total = $query->count();
        $standards = $query->orderBy('id', $order)->limit($limit)->offset($offset)->get();
        $data = [];
        foreach ($standards as $standard) {
            $data[] = [
                'id'            => $standard->id,
                "name"          => $standard->name,
                "post"          => $standard->post,
                "desc"          => $standard->desc
            ];
        }

        return [
            'total'      => $total,
            'rows'       => $data,
            'limit'      => $limit,
            'offset'     => $offset,
            'page'       => ceil($total / $limit),
            'pageNumber' => $offset / $limit + 1
        ];
    }

    public static function getStandardDetail($id)
    {
        $standard = Standard::where('id', $id)->first();

        if (!$standard) {
            return [];
        }

        return [
            'data' => [
                'id'            => $standard->id,
                "name"          => $standard->name,
                "post"          => $standard->post,
                "desc"          => $standard->desc
            ]
        ];
    }

    public static function saveStandard($data)
    {
        $standard = new Standard();
        if (array_key_exists('id', $data) && $data['id']) {
            $standard = Standard::where('id', $data['id'])->first();
        } else {
            $standard->created_at = date('Y-m-d H:i:s', time());
        }
        $standard->name = $data['name'];
        $standard->post = $data['post'];
        $standard->desc = $data['desc'];
        $standard->updated_at = date('Y-m-d H:i:s', time());
        $standard->save();
    }

    public static function deleteStandard($id)
    {
        $standard = Standard::where('id', $id)->first();
        $standard->status = 0;
        $standard->updated_at = date('Y-m-d H:i:s', time());
        $standard->save();
    }

    public static function getPrograms()
    {
        $programs = Program::where('status', 1)->get();
        $data = [];
        foreach ($programs as $program) {
            $data[] = [
                'id' => $program->id,
                'title' => $program->title
            ];
        }

        return ['rows' => $data];
    }

    public static function getProgramUsers($programId)
    {
        $programUsers = ProgramUser::where('program_id', $programId)->where('status', 2)->get();
        $data = [];
        foreach ($programUsers as $programUser) {
            $data[] = [
                'id' => $programUser->user_id,
                'realname' => $programUser->realname
            ];
        }
        return ['rows' => $data];
    }

    public static function getProgramAparts($programId)
    {
        $programAparts = ProgramApart::where('program_id', $programId)->where('status', 1)->get();
        $data = [];
        foreach ($programAparts as $programApart) {
            $data[] = [
                'id' => $programApart->apart_id,
                'apart_name' => $programApart->apart_name
            ];
        }
        return ['rows' => $data];
    }

    public static function sendSms($title, $content, User $from, User $to)
    {
        $role = $from->role;
        $number = $to->number;
        $realname = $to->realname;
        $sender = new SendSms();
        $sender->send($title, $content, $role, $number, $realname);
    }
}